package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.RewardTask;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class HotRewardAdapter extends RecyclerView.Adapter<HotRewardAdapter.ViewHolder> {

    private final Context context;
    private List<RewardTask> rewardTasks;
    private final OnRewardClickListener listener;

    public interface OnRewardClickListener {
        void onRewardClick(RewardTask rewardTask);
    }

    public HotRewardAdapter(Context context, List<RewardTask> rewardTasks, OnRewardClickListener listener) {
        this.context = context;
        this.rewardTasks = rewardTasks;
        this.listener = listener;
    }

    public void updateData(List<RewardTask> newRewardTasks) {
        this.rewardTasks = newRewardTasks;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_hot_reward, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RewardTask rewardTask = rewardTasks.get(position);
        
        // 设置标题
        holder.titleTextView.setText(rewardTask.getTitle());
        
        // 设置分类
        holder.categoryTextView.setText(rewardTask.getCategory());
        
        // 设置金额
        holder.amountTextView.setText(String.format(Locale.getDefault(), "¥%.2f", rewardTask.getReward()));
        
        // 设置剩余数量
        holder.remainingTextView.setText(String.format(Locale.getDefault(), "剩余: %d", rewardTask.getRemainingOrders()));
        
        // 设置发布者
        holder.publisherTextView.setText(String.format("发布者: %s", rewardTask.getUserName()));
        
        // 计算截止日期
        long daysUntilDeadline = getDaysUntilDeadline(rewardTask.getDeadlineAsDate());
        if (daysUntilDeadline > 0) {
            holder.deadlineTextView.setText(String.format(Locale.getDefault(), "%d天后截止", daysUntilDeadline));
        } else {
            holder.deadlineTextView.setText("今日截止");
            holder.deadlineTextView.setTextColor(context.getResources().getColor(android.R.color.holo_red_light));
        }
        
        // 根据分类设置图标
        setTypeIcon(holder, rewardTask.getCategory());
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRewardClick(rewardTask);
            }
        });
    }
    
    private void setTypeIcon(ViewHolder holder, String category) {
        // 根据分类设置不同的图标，这里使用系统图标作为示例
        // 在实际应用中应该使用与悬赏任务类型相对应的图标
        switch (category) {
            case "数据分析":
                holder.typeIconView.setImageResource(android.R.drawable.ic_dialog_dialer);
                break;
            case "文案撰写":
                holder.typeIconView.setImageResource(android.R.drawable.ic_menu_edit);
                break;
            case "设计":
                holder.typeIconView.setImageResource(android.R.drawable.ic_menu_gallery);
                break;
            case "编程":
                holder.typeIconView.setImageResource(android.R.drawable.ic_menu_manage);
                break;
            default:
                holder.typeIconView.setImageResource(android.R.drawable.ic_menu_help);
                break;
        }
    }
    
    private long getDaysUntilDeadline(Date deadline) {
        if (deadline == null) {
            return 0;
        }
        
        long diffInMillis = deadline.getTime() - System.currentTimeMillis();
        return TimeUnit.MILLISECONDS.toDays(diffInMillis);
    }

    @Override
    public int getItemCount() {
        return rewardTasks.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView typeIconView;
        TextView categoryTextView;
        TextView titleTextView;
        TextView amountTextView;
        TextView remainingTextView;
        TextView publisherTextView;
        TextView deadlineTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            typeIconView = itemView.findViewById(R.id.reward_type_icon);
            categoryTextView = itemView.findViewById(R.id.reward_category);
            titleTextView = itemView.findViewById(R.id.reward_title);
            amountTextView = itemView.findViewById(R.id.reward_amount);
            remainingTextView = itemView.findViewById(R.id.reward_remaining);
            publisherTextView = itemView.findViewById(R.id.reward_publisher);
            deadlineTextView = itemView.findViewById(R.id.reward_deadline);
        }
    }
} 